/** Copyright (C) 2007 Pascal Richter
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package jlfm.lastfm.client.impl;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import jlfm.lastfm.client.LastFmClientFaultException;
import jlfm.lastfm.client.command.LastFmCommandResult;
import jlfm.lastfm.client.impl.simple.ProxyConfiguration;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class LastFmHttpCommandClient {

	private HttpClient httpClient;

	public LastFmHttpCommandClient() {
		this.httpClient = new HttpClient();
	}

	public LastFmHttpCommandClient(ProxyConfiguration proxyConfiguration) {
		this();
		setProxyConfiguration(proxyConfiguration);
	}

	public LastFmCommandResult execute(URL url) throws LastFmClientFaultException {
		HttpMethod httpMethod = new GetMethod(url.toString());
		httpMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
				new DefaultHttpMethodRetryHandler(3, false));
		httpMethod.setDoAuthentication(true);
		try {

			int responseCode = httpClient.executeMethod(httpMethod);

			if (responseCode != 200) {
				throw new LastFmClientFaultException(httpMethod
						.getResponseBodyAsString());
			}
			return new DefaultResponseParser().parse(httpMethod
					.getResponseBodyAsStream());
		} catch (HttpException e) {
			e.printStackTrace();
			throw new LastFmClientFaultException(e);
		} catch (IOException e) {
			e.printStackTrace();
			throw new LastFmClientFaultException(e);
		} finally {
			httpMethod.releaseConnection();
		}
	}

	public InputStream getInputStream(URL url) throws LastFmClientFaultException {
		HttpMethod httpMethod = new GetMethod(url.toString());
		httpMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
				new DefaultHttpMethodRetryHandler(3, false));
		httpMethod.setDoAuthentication(true);

		try {

			int responseCode = httpClient.executeMethod(httpMethod);

			if (responseCode != 200) {
				throw new LastFmClientFaultException(httpMethod
						.getResponseBodyAsString());
			}
			return httpMethod.getResponseBodyAsStream();
		} catch (HttpException e) {
			e.printStackTrace();
			throw new LastFmClientFaultException(e);
		} catch (IOException e) {
			e.printStackTrace();
			throw new LastFmClientFaultException(e);
		}
	}

	/**
	 * @param proxyConfiguration
	 *            the proxyConfiguration to set
	 */
	public void setProxyConfiguration(ProxyConfiguration proxyConfiguration) {
		HostConfiguration conf = new HostConfiguration();
		if (proxyConfiguration.isUseProxy()) {
			conf.setProxy(proxyConfiguration.getProxyHost(), proxyConfiguration
					.getProxyPort());
		}
		httpClient.setHostConfiguration(conf);
	}

}
